package SSF.OS.NetFlow.Util;

import SSF.OS.NetFlow.*;
import SSF.Util.Streams.*;
import java.io.*;

/**
 * This class reads the binary netflow data from a file
 * and print it out
 */
public class IpFlowReporter extends BasicPlayer
{
    public IpFlowReporter(String streamID)
    {
	super(streamID);
    }

    /**
     * Grab an IpFlowCollector record and print it on screen
     * @param tid type ID or the record
     * @param sid ID corresponding to source NHI address
     * @param time timestamp when it's dumped
     * @param buf byte array storing the data
     * @param offset where in the byte array to start
     * @param length how long are the data in the array
     */
    public int receive (int tid, int sid, double time, 
			byte[] buf, int offset, int length)
    {
	IpNetFlow flow;
	//System.err.println(buf.length + " " +offset+ " " + length);
	if (tid == getRecordTypeCode(IpFlowCollector.STREAM_TYPE)) {
	    flow = new IpNetFlow();
	    flow.readFromBytes(buf, length);
	    System.out.println(flow.toString());
	    return 0;
	}
	else return super.receive(tid, sid, time, buf, offset, length);
    }

    public static void main(String args[])
    {
	IpFlowReporter ifr = null;
	if (args.length < 2) {
	    System.err.println("usage: java IpFlowReporter <filename> <stream name>");
	    return;
	}

	ifr = new IpFlowReporter(args[1]); //stream name given to create Reporter

	try {
	    ifr.connectRead(args[0]); //Report
	}
	catch (streamException strex) {
	    System.err.println(strex.getMessage());
	    System.exit(1);
	}
    }
}
